package com.li.youhappygo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.li.youhappygo.exception.GlobalException;
import com.li.youhappygo.mapper.*;
import com.li.youhappygo.model.dto.OrderQueryParamDto;
import com.li.youhappygo.model.entity.order.OrderInfo;
import com.li.youhappygo.model.entity.product.Product;
import com.li.youhappygo.model.entity.product.ProductSku;
import com.li.youhappygo.model.entity.user.UserAddress;
import com.li.youhappygo.model.entity.user.Users;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.model.vo.order.OrderDetailVo;
import com.li.youhappygo.model.vo.order.OrderVo;
import com.li.youhappygo.service.OrderService;
import org.apache.catalina.User;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderInfoMapper orderInfoMapper;
    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderReceiverInfoMapper orderReceiverInfoMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public List<OrderVo> getAllOrder(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<OrderInfo> orderInfoList = orderInfoMapper.selectList(null);
        List<OrderVo> orderVoList = getOrderVo(orderInfoList);
        return orderVoList;
    }


    @Override
    public List<OrderDetailVo> getDetailInfoByOrderId(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<OrderInfo> orderInfoList = orderInfoMapper.selectList(null);
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        for (OrderInfo orderInfoOrig : orderInfoList){
            Long orderId = orderInfoOrig.getId();
            QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<OrderInfo>();
            queryWrapper.eq("id",orderId);
            OrderInfo orderInfo =  orderInfoMapper.selectOne(queryWrapper);
            List<ProductSku> productSkuList = orderItemMapper.findProductSkuList(orderId);
            UserAddress userAddress = orderReceiverInfoMapper.findUserAddress(orderId);
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setOrderInfo(orderInfo);
            orderDetailVo.setUserAddress(userAddress);
            orderDetailVo.setProductSkuList(productSkuList);
            QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
            usersQueryWrapper.eq("id",orderInfo.getUserId());
            Users users = userMapper.selectOne(usersQueryWrapper);
            orderDetailVo.setUsers(users);
            orderDetailVoList.add(orderDetailVo);
        }
//        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<OrderInfo>();
//        queryWrapper.eq("id",orderId);
//        OrderInfo orderInfo =  orderInfoMapper.selectOne(queryWrapper);
//        List<ProductSku> productSkuList = orderItemMapper.findProductSkuList(orderId);
//        UserAddress userAddress = orderReceiverInfoMapper.findUserAddress(orderId);
//        OrderDetailVo orderDetailVo = new OrderDetailVo();
//        orderDetailVo.setOrderInfo(orderInfo);
//        orderDetailVo.setUserAddress(userAddress);
//        orderDetailVo.setProductSkuList(productSkuList);
//        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
//        usersQueryWrapper.eq("id",orderInfo.getUserId());
//        Users users = userMapper.selectOne(usersQueryWrapper);
//        orderDetailVo.setUsers(users);
        return orderDetailVoList;
    }

    @Override
    public List<OrderVo> getOrderByStatus(Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<OrderInfo>();
        queryWrapper.eq("orderStatus",status);
        List<OrderInfo> orderInfoList = orderInfoMapper.selectList(queryWrapper);
        List<OrderVo> orderVoList;
        orderVoList = getOrderVo(orderInfoList);
        return orderVoList;
    }

    @Override
    public OrderVo getOrderByOrderNum(String orderNum) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<OrderInfo>();
        queryWrapper.eq("order_num",orderNum);
//        List<OrderInfo> orderInfoList = orderInfoMapper.selectList(queryWrapper);
        OrderInfo orderInfo = orderInfoMapper.selectOne(queryWrapper);
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(orderInfo,orderVo);
        return orderVo;
    }

    @Override
    public void updateOrderStatus(List<Long> ids, Integer orderStatus) {
        for(Long id : ids){
            UpdateWrapper<OrderInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(OrderInfo::getId, id);
            updateWrapper.lambda().set(OrderInfo::getOrderStatus,orderStatus);
            orderInfoMapper.update(null, updateWrapper);
        }
    }

    @Override
    public PageInfo<OrderDetailVo> getOrderByQueryParam(OrderQueryParamDto orderQueryParamDto, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<OrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(orderQueryParamDto.getOrderNum()!=null,OrderInfo::getOrderNum,orderQueryParamDto.getOrderNum())
                .eq(orderQueryParamDto.getOrderStatus()!=null,OrderInfo::getOrderStatus,orderQueryParamDto.getOrderStatus())
                .eq(orderQueryParamDto.getPayType()!=null,OrderInfo::getPayType,orderQueryParamDto.getPayType())
                .eq(orderQueryParamDto.getCreateTime()!=null,OrderInfo::getCreateTime,orderQueryParamDto.getCreateTime());
        PageHelper.startPage(pageNum,pageSize);
        List<OrderInfo> orderInfoList = orderInfoMapper.selectList(queryWrapper);
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        for (OrderInfo orderInfoOrig : orderInfoList){
            Long orderId = orderInfoOrig.getId();
            QueryWrapper<OrderInfo> queryWrapper2 = new QueryWrapper<OrderInfo>();
            queryWrapper2.eq("id",orderId);
            OrderInfo orderInfo =  orderInfoMapper.selectOne(queryWrapper2);
            List<ProductSku> productSkuList = orderItemMapper.findProductSkuList(orderId);
            UserAddress userAddress = orderReceiverInfoMapper.findUserAddress(orderId);
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setOrderInfo(orderInfo);
            orderDetailVo.setUserAddress(userAddress);
            orderDetailVo.setProductSkuList(productSkuList);
            QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
            usersQueryWrapper.eq("id",orderInfo.getUserId());
            Users users = userMapper.selectOne(usersQueryWrapper);
            orderDetailVo.setUsers(users);
            orderDetailVoList.add(orderDetailVo);
        }
        PageInfo<OrderDetailVo> pageInfo = new PageInfo<>(orderDetailVoList);

//        List<OrderVo> orderVoList = getOrderVo(orderInfoList);
//        PageInfo<OrderVo> pageInfo = new PageInfo<>(orderVoList);
        return pageInfo;
    }

    @Override
    public void updateRemark(Long orderId, String remark) {
        UpdateWrapper<OrderInfo> queryWrapper = new UpdateWrapper<>();
        queryWrapper.lambda().
                eq(OrderInfo::getId,orderId)
                .set(OrderInfo::getRemark,remark);
        int update = orderInfoMapper.update(null, queryWrapper);
        if(update<=0){
            throw new GlobalException(ResultCode.FAIL);
        }
    }

    @Override
    public OrderDetailVo getDetailInfoByOrderId2(Long orderId) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",orderId);
        OrderInfo orderInfo =  orderInfoMapper.selectOne(queryWrapper);
        List<ProductSku> productSkuList = orderItemMapper.findProductSkuList(orderId);
        UserAddress userAddress = orderReceiverInfoMapper.findUserAddress(orderId);
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setOrderInfo(orderInfo);
        orderDetailVo.setUserAddress(userAddress);
        orderDetailVo.setProductSkuList(productSkuList);
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("id",orderInfo.getUserId());
        Users users = userMapper.selectOne(usersQueryWrapper);
        orderDetailVo.setUsers(users);
        return orderDetailVo;
    }


    public List<OrderVo> getOrderVo(List<OrderInfo> orderInfoList){
        List<OrderVo> orderVoList = new ArrayList<>();
        for (OrderInfo orderInfo : orderInfoList) {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(orderInfo,orderVo);
            QueryWrapper<Users> queryWrapper = new QueryWrapper();
            queryWrapper.lambda().eq(Users::getId,orderInfo.getUserId());
            Users users = userMapper.selectOne(queryWrapper);
            if(users != null) {
                orderVo.setUsername(users.getUsername());
                orderVo.setUserId(users.getId());
            }
            orderVoList.add(orderVo);
        }
        return orderVoList;
    }
}
